package com.xiaobias;


import java.util.LinkedHashSet;
import java.util.Set;

public class PasswordGenerator {

    // 定义字符集：大小写字母、数字和符号
    private static final String CHARACTERS =
            "abcdefghijklmnopqrstuvwxyz" +  // 小写字母
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // 大写字母
                    "0123456789" +                  // 数字
                    "!@#$%^&*()-_=+[]{}|;:,.<>?";   // 符号

    private static final int MAX_LENGTH = 4; // 最大密码长度

    public static void main(String[] args) throws Exception {
        // 生成1到32位的所有密码组合
        Set<String> passwords = generatePasswords(MAX_LENGTH);

        System.out.println("规则长度：" + CHARACTERS.length());

        double count = 0;
        for (int i = 1; i <= MAX_LENGTH; i++) {
            double pow = Math.pow(CHARACTERS.length(), i);
            System.out.println( "第" + i + "-组合：" + pow);
            count += pow;
        }
        System.out.println("总共的组合：" + count);

        System.out.println(passwords.size());

        // 打印生成的密码（注意：数量可能非常大，建议限制输出）
//        for (String password : passwords) {
//            System.out.println(password);
//        }
    }

    /**
     * 生成1到maxLength位的所有密码组合
     */
    public static Set<String> generatePasswords(int maxLength) {
        Set<String> passwords = new LinkedHashSet<>();
        for (int length = 1; length <= maxLength; length++) {
            generatePasswordsRecursive("", length, passwords);
        }
        return passwords;
    }

    /**
     * 递归生成密码组合
     */
    private static void generatePasswordsRecursive(String current, int length, Set<String> passwords) {
        if (current.length() == length) {
            passwords.add(current); // 添加到结果列表
            return;
        }

        // 遍历字符集，递归生成密码
        for (char c : CHARACTERS.toCharArray()) {
            generatePasswordsRecursive(current + c, length, passwords);
        }
    }
}